import Axios from "axios";
import router from "@/router";
import {Toast} from "vant";
// HTTPrequest拦截
Axios.interceptors.request.use(config => {
    config.headers['token'] = '1111' // token
    return config ;
},error => {
    //console.log(error)
    Promise.reject(error)
}) ;

// 响应拦截器
Axios.interceptors.response.use(res => {
        const code = res.data.code
        if (code==1001){
            Toast(res.data.msg)
            router.push('/')
            return Promise.reject('error')
        }else{
            return res
        }
    },
 error => {
        return Promise.reject(error)
    }
)


export function request(url,method="get",data={},config={}) {
    return axiosRequest(url,method,data,config);
}
function axiosRequest(url,method,data,config){
    let axiosConfig={
        url:url,
        method:method.toLocaleLowerCase(),
    }
    if (method.toLowerCase() == "post"){
        axiosConfig['data'] = data ;
    }else if(method.toLowerCase() == "get"){
        axiosConfig['params'] = data ;
    }
    if(config instanceof Object){
        for(let key in config){
            axiosConfig[key]=config[key];
        }
    }
    return Axios(axiosConfig).then(res=>res.data);
}

